package com.qingxin.wuye.repertory.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.wuye.repertory.entity.ChukuDocket;
import com.qingxin.wuye.repertory.entity.Repertory;
import com.qingxin.wuye.repertory.service.ChukuDocketService;
import com.qingxin.wuye.repertory.service.RepertoryService;

@Controller
@RequestMapping("/outRes")
public class ChukuDocketAction extends BaseAction{
	@Autowired
	private ChukuDocketService chukuDocketService;
	@Autowired
	private RepertoryService repertoryService;
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String index(ModelMap model , HttpServletRequest request) {
		List<Repertory> repertoryrs = new ArrayList<>();
		repertoryrs = repertoryService.listByHql("from Repertory order by name asc");
		model.put("repertoryrs", repertoryrs);
		return "wuye/outRes/danju/danju_list";
	}
	
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String searchname,String repertoryid,PageParam pageParam, HttpServletRequest request){
		return chukuDocketService.list(searchname,repertoryid, pageParam, getLoginUser(request));
	}
	
	
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "wuye/outRes/danju/danju_add";
	}
	
	

	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid ChukuDocket chukuDocket, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = chukuDocketService.add(chukuDocket, getLoginUser(request), request);
		return info;
	}
	

	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String docketid, HttpServletRequest request){
		ChukuDocket chukuDocket= chukuDocketService.get(docketid);
		model.put("docket", chukuDocket);
		return "wuye/outRes/danju/danju_edit";
	}

	@RequestMapping(value="/view.do" , method=RequestMethod.GET)
	public String view(ModelMap model, String docketid, HttpServletRequest request){
		ChukuDocket chukuDocket= chukuDocketService.get(docketid);
		model.put("docket", chukuDocket);
		model.put("repertoryName", repertoryService.get(chukuDocket.getRepertoryid()).getName());
		return "wuye/outRes/danju/danju_view";
	}
	
	
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid ChukuDocket chukuDocket, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = chukuDocketService.update(chukuDocket, getLoginUser(request), request);
		return info;
	}
	

	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] docketid, HttpServletRequest request){
		if(docketid==null || docketid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return chukuDocketService.delete(docketid, getLoginUser(request), request);
	}
	
}
